package com.bytecodes.eaulas.comun;


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;



/**
 * Clase EAulasTableModel: encapsula todo el comportamiento de las JTable
 * Permite mostrar datos de tablas del modelo, independientemente del número de columnas 
 * o de sus nombres
 * 
 * @author David Fernandez Gamez
 * @version 1.0
 */




public class EAulasTableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3691508584124598302L;
	Object[][] contents;
	String[] columnNames;
	Class[] columnClasses;

	public EAulasTableModel (Connection conn,
			String tableName, String filter)
					throws SQLException {
		super();
		getTableContents (conn, tableName, filter);

	}
	public void getTableContents (Connection conn,
			String tableName, String filter)
					throws SQLException {
		DatabaseMetaData meta = conn.getMetaData();
		ResultSet results =
				meta.getColumns (null, null, tableName, null) ;
		ArrayList colNamesList = new ArrayList();
		ArrayList colClassesList = new ArrayList();
		while (results.next()) {
			colNamesList.add (results.getString ("COLUMN_NAME")); 
			int dbType = results.getInt ("DATA_TYPE");
			switch (dbType) {
			case Types.INTEGER:
				colClassesList.add (Integer.class); break; 
			case Types.FLOAT:
				colClassesList.add (Float.class); break; 
			case Types.DOUBLE: 
			case Types.REAL:
				colClassesList.add (Double.class); break; 
			case Types.DATE: 
			case Types.TIME: 
			case Types.TIMESTAMP:
				colClassesList.add (java.sql.Date.class); break; 
			default:
				colClassesList.add (String.class); break; 
			}; 
		}
		columnNames = new String [colNamesList.size()];
		colNamesList.toArray (columnNames);
		columnClasses = new Class [colClassesList.size()];
		colClassesList.toArray (columnClasses);

		Statement statement =
				conn.createStatement ();
		String sql="SELECT * FROM " +tableName;
		if(!"".equals(filter)){
			sql= sql+filter;
		}
		results = statement.executeQuery (sql);
		ArrayList rowList = new ArrayList();
		while (results.next()) {
			ArrayList cellList = new ArrayList(); 
			for (int i = 0; i<columnClasses.length; i++) { 
				Object cellValue = null;


				if (columnClasses[i] == String.class) 
					cellValue = results.getString (columnNames[i]); 
				else if (columnClasses[i] == Integer.class) 
					cellValue = new Integer ( 
							results.getInt (columnNames[i])); 
				else if (columnClasses[i] == Float.class) 
					cellValue = new Float ( 
							results.getInt (columnNames[i])); 
				else if (columnClasses[i] == Double.class) 
					cellValue = new Double ( 
							results.getDouble (columnNames[i]));
				else if (columnClasses[i] == java.sql.Date.class) 
					cellValue = results.getDate (columnNames[i]); 
				else 
					System.out.println ("Can't assign " + 
							columnNames[i]);
				cellList.add (cellValue);
			}// for
			Object[] cells = cellList.toArray();
			rowList.add (cells);

		} // while

		contents = new Object[rowList.size()] [];
		for (int i=0; i<contents.length; i++)
			contents[i] = (Object []) rowList.get (i);

		results.close();
		statement.close();

	}

	public int getRowCount() {
		return contents.length;
	}

	public int getColumnCount() {
		if (contents.length == 0)
			return 0;
		else
			return contents[0].length;
	}

	public Object getValueAt (int row, int column) {
		return contents [row][column];
	}

	public Class getColumnClass (int col) {
		return columnClasses [col];
	}

	public String getColumnName (int col) { 
		return columnNames [col]; 
	}
	public Object[][] getContents() {
		return contents;
	}
	public void setContents(Object[][] contents) {
		this.contents = contents;
	}
	public String[] getColumnNames() {
		return columnNames;
	}
	public void setColumnNames(String[] columnNames) {
		this.columnNames = columnNames;
	} 
}